package 面向对象.TestEight;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;

/**
 * @author IT_Jay
 * @date 2021/11/20 21:42
 */

public class Frame1 {
    public static void main(String[] args) {
        MathWindow window = new MathWindow();
    }

    static class MathWindow extends JFrame{
        JTextField text1,text2,text3;
        JPanel ps,pn;
        MathWindow(){
            text1 = new JTextField(10);
            text2 = new JTextField(10);
            text3 = new JTextField(10);

            JButton button1,button2,button3,button4;
            button1 = new JButton("加");
            button2 = new JButton("减");
            button3 = new JButton("乘");
            button4 = new JButton("除");

            ps = new JPanel();
            pn = new JPanel();
            pn.add(text1);
            pn.add(text2);
            pn.add(text3);
            ps.add(button1);
            ps.add(button2);
            ps.add(button3);
            ps.add(button4);

            add(pn, BorderLayout.CENTER);
            add(ps, BorderLayout.SOUTH);
            setBounds(100,100,370,150);
            setVisible(true);
            validate();
            button1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String s1 = text1.getText();
                    String s2 = text2.getText();
                    try {
                        BigInteger n1 = new BigInteger(s1);
                        BigInteger n2 = new BigInteger(s2);
                        n2 = n1.add(n2);
                        text3.setText(n2.toString());
                    }catch (NumberFormatException e1){
                        text3.setText("请输入数字");
                        text1.setText(null);
                        text2.setText(null);
                    }
                }
            });
            button2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String s1 = text1.getText();
                    String s2 = text2.getText();
                    try {
                        BigInteger n1 = new BigInteger(s1);
                        BigInteger n2 = new BigInteger(s2);
                        n2 = n1.subtract(n2);
                        text3.setText(n2.toString());
                    }catch (NumberFormatException e1){
                        text3.setText("请输入数字");
                        text1.setText(null);
                        text2.setText(null);
                    }
                }
            });
            button3.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String s1 = text1.getText();
                    String s2 = text2.getText();
                    try {
                        BigInteger n1 = new BigInteger(s1);
                        BigInteger n2 = new BigInteger(s2);
                        n2 = n1.multiply(n2);
                        text3.setText(n2.toString());
                    }catch (NumberFormatException e1){
                        text3.setText("请输入数字");
                        text1.setText(null);
                        text2.setText(null);
                    }
                }
            });
            button4.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String s1 = text1.getText();
                    String s2 = text2.getText();
                    try {
                        BigInteger n1 = new BigInteger(s1);
                        BigInteger n2 = new BigInteger(s2);
                        if (n2.toString() == "0"){
                            text3.setText("除数不能为0");
                        }else {
                            n2 = n1.divide(n2);
                            text3.setText(n2.toString());
                        }
                    }catch (NumberFormatException e1){
                        text3.setText("请输入数字");
                        text1.setText(null);
                        text2.setText(null);
                    }
                }
            });
        }
    }
}
